using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Web.Model;
using System.Linq;

namespace Web.DataAccess
{
    public class NewsSortedByDateQuery
    {
        private User CurrentUser { get; set; }

        public NewsSortedByDateQuery(User currentUser)
        {
            CurrentUser = currentUser;
        }
        public IList<News> Load(ISession session)
        {
            var news = session
                .CreateCriteria<News>()
                .AddOrder(Order.Desc("DateCreated"))
                .List<News>();
            if (CurrentUser != null && CurrentUser.IsAdmin)
            {
                return news;
            }
            else
            {
                return news.Where(n => n.IsVisibleToUser(CurrentUser)).ToList();
            }
        }
    }
}